package GameLogic

import (
	"git.oschina.net/yangdao/GameLib/Area"
	"git.oschina.net/yangdao/GameLib/Unit"
	"git.oschina.net/yangdao/SGLisp"
	"git.oschina.net/yangdao/SGLisp/parse"
)

type Effect_SearchUnit struct {
	*Unit.Effect
	Area       Area.IArea
	ExecEffect Unit.IEffect
	cacheArgs  []parse.Node
	Scene      *GameScene
}

func (this *Effect_SearchUnit) Base() *Unit.Effect {
	return this.Effect
}
func (this *Effect_SearchUnit) OnLoad(srcAbility Unit.IAbility, srcUnit Unit.IUnit, otherArgs ...interface{}) {
	this.Effect.OnLoad(srcAbility, srcUnit, otherArgs)
	this.ExecEffect.OnLoad(srcAbility, srcUnit, otherArgs)

}

func CreateEffectSearchUnit(args []parse.Node, context *SGLisp.Context) Unit.IEffect {
	newEffect := &Effect_SearchUnit{
		Effect: &Unit.Effect{},
	}
	newEffect.cacheArgs = args
	newEffect.Name = args[0].(*parse.SymbolNode).Val
	newEffect.ExecEffect = Unit.EffectManager.CreateEffectByLisp(args[2].(*parse.ListNode))
	return newEffect
}

func (this *Effect_SearchUnit) OnExec(context *SGLisp.Context) {
	this.Scene = this.SrcUnit.Base().Scene.(*GameScene)
	this.Area = SGLisp.EvalNode(SGLisp.Evaluate(this.cacheArgs[1], context), context).(Area.IArea)
	//fmt.Println(this.Scene, this.SrcUnit.Scene, "Scene????")
	this.Scene.EachUnit(func(unit Unit.IUnit) {
		gameUnit := unit.(*GameUnit)
		if this.SrcUnit.Base().ParentUnit != nil && this.SrcUnit.Base().ParentUnit.Base().GUID == gameUnit.Base().GUID {
			return
		}
		//fmt.Println(gameUnit.Unit.GUID, gameUnit.Unit.Name, this.SrcUnit.Base().GUID, this.SrcUnit.Base().Name)
		if gameUnit.Base().IsDie == false && gameUnit.Base().IsInvincible == false {
			pos := gameUnit.Unit.Pos()
			if this.SrcUnit != gameUnit.Unit && gameUnit.Unit.IsHero && this.Area.IsInArea(&pos) {
				this.ExecEffect.Base().DstUnit = gameUnit
				this.ExecEffect.Base().SrcUnit = this.SrcUnit
				this.ExecEffect.OnExec(context)
			}
		}
	})
}
